//
//  focus.swift
//  focus
//
//  Created by 恒 on 2025/7/15.
//

import WidgetKit
import SwiftUI
import Foundation


struct CircularProgressView: View {
    var progress: Double
    var color: Color = .accentColor
    
    var body: some View {
        ZStack {
            Circle()
                .stroke(Color.gray.opacity(0.2), lineWidth: 12)
            
            Circle()
                .trim(from: 0, to: CGFloat(min(progress, 1.0)))
                .stroke(
                    AngularGradient(
                        gradient: Gradient(colors: [color.opacity(0.7), color]),
                        center: .center,
                        startAngle: .degrees(-90),
                        endAngle: .degrees(270)
                    ),
                    style: StrokeStyle(lineWidth: 12, lineCap: .round)
                )
                .rotationEffect(.degrees(-90))
                .animation(.easeInOut(duration: 0.5), value: progress)
        }
    }
}

struct SimpleEntry: TimelineEntry {
    let date: Date
    let progress: Double
    let remainingTime: TimeInterval
}

struct Provider: TimelineProvider {
    private let suiteName = "group.com.focusOn.timer"
    private var userDefaults: UserDefaults? {
        UserDefaults(suiteName: suiteName)
    }
    
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date(), progress: 0.5, remainingTime: 1500)
    }
    
    func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry = SimpleEntry(date: Date(), progress: 0.5, remainingTime: 1500)
        completion(entry)
    }
    
    func getTimeline(in context: Context, completion: @escaping (Timeline<SimpleEntry>) -> ()) {
        print("SharedDataStore.sharedDouble : \(SharedDataStore.sharedDouble)")
        completion(Timeline(
            entries: [SimpleEntry(date: Date(),
                                  progress: SharedDataStore.sharedDouble / SharedDataStore.solidTimeSecond,
                                  remainingTime: SharedDataStore.sharedDouble)],
            policy: .after(Date().addingTimeInterval(1)) // 15分钟后刷新
        ))
    }
    
}

struct focusEntryView : View {
    var entry: Provider.Entry
    
    var body: some View {
        VStack(spacing: 12) {
            CircularProgressView(progress: entry.progress)
                .frame(width: 80, height: 80)
            
            Text(timeString(from: entry.remainingTime))
                .font(.title2)
                .fontWeight(.medium)
                .contentTransition(.identity)
        }
        .padding()
    }
    
    private func timeString(from time: TimeInterval) -> String {
        let minutes = Int(time) / 60
        let seconds = Int(time) % 60
        return String(format: "%02d:%02d", minutes, seconds)
    }
}

struct focus: Widget {
    let kind: String = "focus"
    
    var body: some WidgetConfiguration {
        StaticConfiguration(kind: kind, provider: Provider()) { entry in
            if #available(iOS 17.0, *) {
                focusEntryView(entry: entry)
                    .containerBackground(.fill.tertiary, for: .widget)
            } else {
                focusEntryView(entry: entry)
                    .padding()
                    .background()
            }
        }
        .configurationDisplayName("My Widget")
        .description("This is an example widget.")
    }
}

#Preview(as: .systemSmall) {
    focus()
} timeline: {
    SimpleEntry(date: .now, progress: 0.5, remainingTime: 1500)
}
